package cn.com.bluemoon.daps.common.datascope.log.holder;

import cn.com.bluemoon.daps.common.domain.UserInfoHolder;
import lombok.Getter;
import lombok.RequiredArgsConstructor;


/**
 *
 **/
public final class ProcessLogHolder {
    private final static ThreadLocal<ProcessLog> holder = ThreadLocal.withInitial(() -> new ProcessLog("", false, null));

    private ProcessLogHolder() {
    }

    public static void set(ProcessLog operateLog) {
        holder.set(operateLog);
    }

    public static ProcessLog get() {
        return holder.get();
    }

    public static void clear() {
        holder.remove();
    }

    @RequiredArgsConstructor
    @Getter
    public static class ProcessLog{
        private final String content;
        private final boolean fail;
        private final UserInfoHolder.UserInfo userInfo;
    }
    /**
     * 追加日志内容
     */
    public static void set(String detail, boolean fail, UserInfoHolder.UserInfo userInfo) {
        holder.set(new ProcessLog(detail, fail, userInfo));
    }
    /**
     * 追加日志内容
     */
    public static void append(String detail) {
        ProcessLog log = holder.get();
        holder.set(new ProcessLog(log.getContent().concat(detail), log.isFail(), log.userInfo));
    }
}
